* ----------------------------
* Procedure
* ----------------------------

*the main procedure for the simulation
*here we load all data, do the regression 
*and (for the direct effect) predict.
*using auxilliary files we predict the patents
*and when appropriate recompute spillovers and stocks

* 1.a Implement global settings and load data
* 1.b Run firm-divison level reg
* 1.c Initialize Spillovers
* 1.d Implement wage change, subset data for simulation

* 2 Run simulation

* 3 Combine runs, save results

* ---------------------------- *
* ---------------------------- *
* 1. INITIALIZATION            *
* ---------------------------- *
* ---------------------------- *

* --------------------------------
* 1.a) Implement Global settings
* --------------------------------

di "=== Implementing global settings ==="

* the inputs below come from the launchers (batch_keep... or 00_r_r... or 01_r_r_...)
*they are batchrun, wagechange, stocks, spill, with the last two indicating if they are being recomputed or not
do ${d}code/macrosim/set_globals.do "`1'" "`2'" "`3'" "`4'" 


* --------------------------------
* 1.b) Run firm-division level reg
* --------------------------------

di "=== Running regression ==="

*defining the RHS of the equation. We do an interaction with 
*the division indicator for each variable.
*Division has two levels of technology: auto95 and pauto95
*this therefore runs 2 regressions in 1.

global eq ""
foreach var of global spec {
    global eq "${eq} i.division#c.`var'"
}
di "${eq}"

*run the regression, note that we lag the LHS variables onles only here, not in the data
sort firm_division year
ppmlhdfe F2.depvar ${eq} if year>=1995, absorb(${absorb}) vce(cluster firm_division) d

    estimates store joint_model
        esttab joint_model using ///
            ${outd}/Model_${ln_vers}${div_vers}_${chosen_spec}${FE}.csv, ///
            margin b(%10.4f) csv se(%10.4f) nolines noeqlines nogaps replace ///
            scalars(N N_g year) sfmt(a1) type numbers

* ----------------------------
* 1.c) Initialize spillovers
* ----------------------------
*this step should end up with the data in the correct format for the simulation
*NOTE: despite using the recompute file, we end up with the data. we just change the form

di "=== Initializing spillovers ==="

qui {
    if "$spill" == "recompute" { 

        * initialize spillovers (go to BvD-div-ctry-year level; merge weights and multiplier)
        do "${d}code/macrosim/spillovers_init.do"

        * recompute spillovers (build kspillsown variable)
        do "${d}code/macrosim/recompute_spillovers.do" depvar
        
        * aggregate to BvD-ctry-year, redefine firm-level spillovers
        do "${d}code/macrosim/implement_spillovers.do"
    }
    else { 
        * if spillovers are not recomputed, we can stay on BvD-Div-Year level
        gen ctryid = 1
    }
}

* ----------------------------------------*
* 1.d) Implement wage change, subset data *
* ----------------------------------------*

di "=== Preparing data ==="


* Implement wage change (across years)
replace ISP = ISP + log(1+$wagechange_amount)
    
* Subset data for simulation

    *when we recompute spillovers, we need to keep the weights, multiplier and the full real patent count
    * (count_full, may also be referred to as flow) in a country in a year as well as the original stocks of a country
    *for comparisons sake.
    if ("$spill" == "recompute") local spillinitvars "weight_* count_full outsample_mult data_ctrystock"
        else local spillinitvars ""
    
    keep BvD year firm_division division ctryid ctry /// 
    depvar k0 _ppmlhdfe_d ${spec_${chosen_spec}} ${absorb}  ///
    `spillinitvars'
    
    *this is the final save stage before we start the simulation
    *up to here everything in the dataset is the actual data
    *afterwards we have predictions (though the data is still present in seperate variables)
    sort firm_division year ctryid
    *setting state seeds for the simulation
    *Why? we originally had some randomization steps before this from different thing swe tried that we took out as they were not used. That changed the state of the random number generator.
    *in order to perfectly replicate the results here, we set the seeds to be identical with when we submitted the paper.

    di "$spill"
    di "$stocks"
    di "$wagechange_amount"


    if "$spill" == "recompute" & "$stocks" == "recompute" & "$wagechange_amount" ==  ".1" {
        set seed XAA9079b9a86b8df1dec733cb81aa2980c0ab40b4a26465622c7c4d8fff8f5e79cb97b96f08deea52426887ddfe2e5edc4a456e2a5caad51d2f8bd25f3fc0e9622489982a6c9cbfa06bb654a0f6dbb16355df350ff437ee4763a4bd462807ce57a17064e0a7afb24bd0c6425846841c600949407d6f578937052cf0210076a3bc8ceb7cf282d3efe09c2cac69526371016d1fd364786b822c8adbb33eb46d55674ec3515b4ddd0ce8ade14a7c1e10b421b81284b0b038e0ef24f0eb2d8c5320bc9833030a7d568e114b8ec1af882606d7a6d965a452380f083127f83e3ab8559b05eaa89d436a286c942b197b2ac99934addc9db598e07305043b450872f923e4c7cdbb3a5c9b7bee62f1e48a200980d9c508de3742679a8523ab687ec175e1a6420127a03c3f81a32eaff0f248270ee7f805f86301d4bf2a9626b0421f8defbadc74c0a26f9a1e8f9b6252093d654374bcb69fa933a2b42adb08a2b8c867e72803c0847bbd5532e5e78ce53ddd885ac38cfc751cb695250775433570476eae4d52f4ee4ae015ed2c2b210bf2494e4eaaf5bb4fd3f913e4d12079109d2c81653660d3681096cb554948e0ed6e4385fde5a0e66f59d4e0a4eaf853bdfbb5b86ee639f4fe000cb29955d65bdabeb27a381a3ca36ca8701b2ce1dcfee82cb589b0c322df77a6c7b9ff37737b11a19dd257ab05ca5dcb083fdba3d85dad2362edc47d570f40b7ef978f05220f22978d4265dec03fad9d00a129790187a60e12be9782ac3dc2a5d53a7d34aee472e9ceb93fc463408768c6958d31d5c0ca7f97ce4ad5b6251ca0e157d4f8b04bbd380d4994c70a7bfc0c3b16f79712863498d2d8f34adf1639335dd8d0629d89aa4dade2e7eb76e37c738340948fea762a267007d857d5306aef186e2410654317f80aeba79278164bb42bbeaffe0b2406715e182ed7bc75bf2e16eb2fbc42627a2bd2121e862a759c3345aea575f27bbf96a6b0b9b753b1c9a273f33b7db94bb31506c3d562391c14b63ae94f42ad5a52cdcd9637ecece376b284c1059feee30d9f5bc061fc0e605c7c66bc62563fe9d7c8d3710d131bc23c25bbf2aee3b99d66e2a36c050ac20ca85c2a7e2dadba7da90bc6553ae9320e7329a7bf3908dc1a9f0bad5d9f2f0aac6711ca75a208f971103d6acdd81b0e1360e09833ba47d988248beb175ff406c8fd255544550ba85c979f0cd37d83accd55bbbf89bc890f0b916fb87b585212845cd258ee695764a946bafc5b2f634c767663039d5961980d9358ad97791ecf0a1fc679c5372f02c6f522c37b255859045b1eab53533e18bbb3a58e4e063e4250953ade5fe74e9ddb40b9a6ebcd66154406f954fa65a630fd250ab8d205b8ccbe13748dfbbeaa77dac24995a17fd570943f268f01704d6990cfaada3cee07cc921fc27776982e0363a568fc8dfe99c3f28e07a93514a337f3e283540e8cebb63a78dc35e6f647739bf0e74d91ad4de3d1e4328b70d281090df62e2a09b4a624d81ef7e4641321c7e722a2e474f1943a4ce7eb88ef0cfb223da7b8885b21fb35c2119f8c34eb24e8298fa1b4c592d81daacaf9017eba822b3dc32c24c71a5e064ab073083483a7992358cc43da4d43a6a4d0350450186c5c8270d50a1eb29fcd9652daa90a837a61e869e8db3df69555604c5acc9f8c24198d51d124def230fea042180b664713979e80914d30a5ef74cc3270cec88d111d4a330590694c83052c364f345d6f3880c186bc2731d657e05bedf44c9f7c72dd8ba9fe6093cd298f1ea347b5e5f16c9fff544d4ea4f5fc3e5476a5e53ecc38c9999979eb6b746dac76b21b635890ceb2e97a412dbb4cec44eac62d4071a8457d98944c50310e9b22549037fb050e26863b7da09a763acab1a2a2bf571a747eb374a26450da7a2368b32d0d8b7b98b82329f85fd177ff4586c095a007f7a760afb77dc97a5ed6bd859242118c7ce056c64eb39e38969bb4ae74cbe26a1581d62726d7ac1f7bb8d2dc11e40f94de90fe97e9342dff2a171bb30313dd03ec2431eaad512fb3cb96d402a7c6a63d9c5de413263864a27c975004c414982b258cd43e7f40780d7721b68a494a0f316f1f650d91b579e8b7e3f4d6704bc747514acdccfa35d01fd5d207eec1b177eda5c6e24a8ec191e9e6bbc3b2a8cad473dd570fbb221bec62e8de2f25721c536df3766bc4cd94c112dde1ffa214452fb7f87a552874e824c49744dc4d8870c740359dc236394545f0682a6b6f014437039325fe19a08cba85f2ed888f54ce1665d3d6b33e4e0ad7803f863d5e66779f4023925fd21452143b228d765b711fa19998452fa3537e2b4783fb67456ebafceda04e2bc56a166063954a114afba7e3bc0501cedcad36e1c6edfe025badf359c9d48ddca25f8c64395e8cd3ce2092e723ce72e27ab20fba5836bee325e7b8b44b2cee4ae1e9f82670e1a47bbea70f2c6bfa658ad913f4e351b71085a99a6c84bc5a1d6fa91a76212a3bef7dfa97479969111d4fa3af5f343e686d35662f152f24cf5ca4c4e917ce80b6b5b44c81df9e5abe90a9cef76e3997dc0178a0f736b8fc9e94a5c2783c83fd82f5b671ea7d1a9e2c853b9e6f08a18a94d839568e4c17c392d5e39f91dd0bacde969243434849b1bcbdd15bfe59ba906d245773f4dfbc3daf53a7d936fe0b37bc0641e0646255e7cc29ff084a1a67673f6116095fdf302eec1fced4278effb0dc2c103301df93bda74f8e5a60c4ff18f1ad0a1d680ec5b4dcc86f8c9a20ada6a856bc9a5eca143d11e41a1981c78d36f3e329446abb72c95e8da4364d6daebcaeadd0c3e3179bcafcd1c72b77cb720760f76854794478bacff07bf753876bf8745f9bc40c9fba68de0f51efff06aab4020a12af43d032ee75f9d771443b9f1748528016267a828b7cdba41005936e4eaa3e68e4812ee66826f8039abace09724c5d35423620c725ca9d9754731bd47ae4cc4edd05b07eeda1201faa3724fcffc54a26714854189b427649cf389ece259fdb31749d2de207aacd700cc8ee6156544f033a2bb512fe53842c08f8b2c6279232e218a6cf34e0b9db6de16f40c20a5f8e306b9789cbf8dbd40e6122758eb0ee35d256825f306255f02ed45479dea00e8dabdb93dcc86b4099479d20162124f3889a77c78cd3356c7bb4d2a611beec1dc8b4ec4ea37a4f128fd6f7cfffce04a39b5029f4fca13556de16878060f647db40f254bcaeb8aac1380e7031d78c4405eddd330fd3672f6c4c567b03ff70b6a920308bc17ec3d9cfe457c54709cf9ed2d8101c9c4578687275d13261579092e46f4ef88734dd8055528be0e83e589166a1c97cea0f1bf3c72c5ffd78a937737e9151420f874a7b2c775248c1a9a8d4babd2533e50c6d26aee6d88d2dccdf8c1f0507a3353afb1c66a4e197459ceedce40848ebd2f30fcb46b88280e4e70fdf08a49769c3c5a115c02ccce1121d42517ca3de917e243887ac1ef4026ca0b6318184620487faca35b8a94dfb955aaad80410af0f0001000000a81753
        di "seed updated"
        local comp = "seed1"
    }

    if "$spill" == "recompute" & "$stocks" == "recompute" & "$wagechange_amount" ==  "0" {
        set seed XAA05b3934eb8d8bf01d68bf8d0c1860421314b366f184063de271ca04037da07d479602375718b861c3967d71657ca5dc6ed17877bb4a89dcc193fb94425bcbe5d922b1ae663367241df76f2a2d098739edc5cffb372aaaf36457772ff538c7a05f276e49b03b654a3619430d3d27d7816a5cc0dc99999ca3c364524fa52daa993261aed802b1929ef5612d35d4db4e3e7d4d54682276ed443389467376ea5faaf2b2fb26c6fb52ba055171fd00d0a1dd5c18a95d947149a0f266cc0c9e63c8ec40f279471e8c48469f24ee9d5445429b3d1d730467fa3698183e53c2506656490d713855bd16ee07621c77fd3bf6fd58b030690f992bd6f47732f1b1a6524240896d284ba4bc1edc0d92b90d41824fa5c03ffe5a26f2526034c98aca3e202514eecb6633803173a08237f01dfb810a285dd2f9dacfd22c397146e39dacebf10be3b7b30b3f6fb9bf11cdf4dfe8138710477e6aeec377756f5a4430c8c8df800fd809b6952a869260a589f2e14ee99e5a9a894429e836217ae9ad0bed61ef8daa46d70a6b6c970d6f2466aa0d374d94f765cf8cd8fea84c4700ecc0de6b66ada96661f378853dfbcc54588662ed50db13c0e4180fd554a3cfe0884b48dc77226eef6b488ec8127cf17b861bdf22ab02ba9e5ce2ce8a887754d437fa316fe5b51882f83a2c5359d668d9e83907e5ecfe654b5afb1c647eeba1a69cc4d7f44bfca382d63653ec9b796072efc08a64ad7fb17fee4a0276fbccca75a6695176a50e91ef6a218ae630d843884600c8bd66bd63418b01597fa5fd45e2c0753c5ac5966888272fb5e18ef4bbc634d0104e9f18fd541d777091eece9258c8d967786e4e4837b73d725a25c4dcb3653e0a75079e845ee9345db33812ecaa1b20101d14e02580d7ad6c6dcb5986ad549294e0a9be01105bbd2d3b1cfffd0815102aec05ced3d69cffd96595cf4b6dc373c5f2545a3bad342bdb80cb5a0f0cf6857a012451e9132553f705814f6a49951b4608fed01ca2e3a606780ac9292b330d07c6fa2a5be4d1ebf1408837ba6a9acc1920c6382b9797f377765476de167c7e0dfb43479e274b0208a0e96cc9d9dbbdbdf70e96658be1d7926dfeedbf86e2535c560cbd7f581a8edd8c6a8f7ec75d8b728753c839a6922686603df3e134fea76de96dec5288f2c994ab45b3079eaa261333b7575b15f1f0c473dbcc6b5bedeac911ccc2985120a0c7d6219768662264d5ed913383bdf327f76c9f5a3ad33b83f84325c1a26cc83cd5148309581e0aefbffaf4f54b4050d9bf1c32ceefa0ed420e6dbe0a2d2b840a4bbadea709b61bc5d042f4484702489530bc3daaa2391cf79d00fd82d9491363a0cd675124173e6bbf9d8b2c0fdaf85d3370e1ac35e83ba6f77a55b4fe51e9370b1577688b95a219e863f2a862a18bb837c3542059a8b19e1caca956aeb6697818af2501a06b438f7371eacdc55550f969e76a6fa2f60ca417afbc0608229f6b0156d3accca3567793a08322ccc01bb02a2fb0e23da34b84794144eb1450f4f72d8b4c77da8c186e2eb62c9e0d1aec1f0ce1eb10bada36c9383bfbac1a1bd6cb31dc4c7e951ff088a1ab9d55c0b59d88dfd3597edc1950916b91cae55edb96f37fbd1d832664c563fdb3c142db9e0a65bd2b34055148b60bc9e188c8709854f999d84856373eef3f9eccc54ca9b46c562b2af200bb35cc471898d375fdea7b873a1b0bb6a9b0221b79392b1d2c529d5fae596b5de680b8deeef6fa808b8e2a53bd4c402f630b501cafb5bb24b1428e0230f827b0d6f4f0cb61e158d886a64383cc6af289012cdc587320e3ab83067d22bc290a3a50ef726eec18b683d4062b746823052a7a0707d739ca8f474aa832c06d908254c418425b1947aaa41395f5c7870cf3fef70383c2cacaf096c051753bd6ac150643da13525540d9e1e8ba810306d3cb6a16220d80a3efa0ef3ce76db11dfd7f72a293b4e5cdae6f9175b5889dde1dfa84f5891d71e03587c3c56597440e2e764cb5957c73a8ba0ae7f72c38cfef34b73a61fff74db0e69506dbb3484fdfabbb40d73ce47cc3892b5ccf6afbaff20d3913c578eff268219d722e60b9b50a83cd040089e2ebe46de3e43b7000a476b68517c1f90a4d6c8294580c83e31955dfa7e757bec11c45db13c8f197e6f990ab6bc980a43f5f606ba92e67528a9ca40e12562471db4e095a5f036dc8905bef9c2cea3efad39f2fffa092ee353101a5874fa74a18aef6b0bf9c7a264b48019e437e61a85a70821acf64be98d08aff0b2cd53b1d548a7f02cd38c1bab3685d002d18f040239fbdb87cdeab126c09ba61dfc84569313835ce70644c6c1ee3fcd979f8c15f3a106e8070d6d20f33a2f6406931ad2bd31d24f9e9ccd1f491036c705320521849361c7549c63c1d77026b2d1692994fdedb98c491f7061d3044e4121e819047fe706ac357b86db3bb2959ce65a69b6c1d69024616bc1d323cd26383f23bb776c815abdbda1b41dd6a85cfc22766b075d4da2c3e6b3c7c6fae10a3f171b52740ea69aa21240c35843319f66d532b910c3b281c5393963ce86017d206da628702a8f1e9a168523f587a855fba2d85270369fdfa708ad9b4204dc1c8b113a492cb0e2d6336dc30f951078e2e8f59f7d07b3467db90d191f7b82301a97dafd19edecf16c5ab949777728b63c471ef377bfc9dfd12eed7e098b4a8575824d11527b312f353946aa6aa60316d7ac5c83f70499b5cb9c8dfc1e17525e5f118ef2b28c6e940ef3b1abfc861d30e6988857516b886d5b06f4b586373ae02ca539a927ae6c61d2888bf3ca5a0d11a9e5d181452d94c09d7a602502a6e5f7ab46c338d6b04b77e938c41e49b190c14536a76550ec2e22dc303637dfcf8828ca239a54a75b3913cf2bc011cc9b12243d46c245f321c80bb908d4c044d02ab63e2569031c475e24d7c30a8c68cc82575e453eb08ee2827c2b0464a6262c21b5f174718425b20e44aefa24aaf210d37bf676658575e019ab481b70a804600ad1e7249ac9e81d916eebed58bacbd1c29101ac56a4de879f56e228a23244d90ac0d800dc0a6a6a73c8a9d99e921da208373a215827cf202782b566eeba3f35b92bc3a4195ba159ed01c37d51faeb4cbeec27d26c9374a5826d363f72de9b256bf7f73c0460bf4108aed96ad3168d1d69e85da1a34014c938ba134d83dacc2ae219f731989129540e6e7dcdff55797ebc43f683dd94a1f38555ab223b087caa6b3e458a3442a102f51a35a8b02cf24002a9b15db3fd02a413c99cbe7dd6e7055cef2ad0dc18655317b0d31149823904f732e922bbd755b2a98e2e2dd272f691a34dd82620357763142afde518bd7542735a5d8950fe63cea4d72cf5bdc595c77294098d4eeb5deca118a78be3d4b0ad62a05b9be4cb3f12cefaf5aa9113e2bd63f9edf59acb510fc69f3305256ff8d3294d2704e882a5a18c5987e0408a0aed18675a14791bfb3891ba9007a946886d6d65d4dccdf222331f8d61463ddaa24ed100010000005e4170
        di "seed updated"
        local comp = "seed2"
    }

    if "$spill" == "keep" & "$stocks" == "recompute" & "$wagechange_amount" ==  "0.1" {
        set seed XAAc74453b136205322f0e44f31a32481711a3f4275d13555286e8d8f89925a126dacee0951859f241dac737011774e86de8598d3c105377a0aea2291d2f98756cfc4478ae02065b904948781469a4f7c25b7b768b640b689886f4e45e67482924073b187fb78b0217946de64191b9c295e1e7bb6946792cff260bf5bf722e7982259ddd3eeb23c71070e2cb60d9cc9aacf65c75c368a914e29471869a00f477ba2ffa8c973e774a2480680378dc0a63653ec4fdb9de6d01eae85b02abd4406b1c77e80fb1b65ea71f7a4e976e45f133098b12d3af3fc79f802d44c1b968ebcc7ac8f586406a1dc7f8ae0fc66ec96535a3fb8a4f8808058db6417bd5e8027e25920247f6ea195c0ac613a2521ded02cdba5d0c5f8e98cfe2358e6146ff2c36374606ef977ff60499a5d4e746c4c063124862a2a66d0a5d72554f395f4eb1dcb6cc2b2293488afa9ce47f930c3b707e799f87af27c382732c45521d31ce9027ccf0c4f32013c7db70eb7396515013953d07c3cf3774f1bbc7facef7999071ea478ff5e3d64529f1994dbec96689744f5210e7d942827d155af44b06953954e7f6681000cde4192625b0175a3b44e22ef4f1ae4b8a57c0989b6094301ae5354727881c71cf09f0396d64b5a0ef293edb9ffa972f4b11fcdc640bf1b10b836131773dd98d195146ddec5684a421f93cacf166dcf492d26a7edc2551ab24f19ea5995bd90c5f9ea46c1e4453e617217200d6da427b3c9c7b1d3e5959c510090885942a0d64a0fa8cd58c69409f8770737e5b59e88e8a4d8be9d0bd663e0347f540963314e943566dafc0bc9b12659d099ba6dbc9eb4cfbc94e5b1e5562231682d36cee63111c4482a12fc2720269402ed5ed937fd86b53a4336c354599cbb0113a4f2a3d2a7b0fcd63e2284ac7a586330f75f33519c534107c29c45d43fb9e7a3411d5a9d5878b4f44b2ebdfa9761c882ca5c391c5b63c80e0186256cd3227c0bbcc63f705f4a4f29dc3afaf0208e8811b93b9e03c3059828fb1d1df87158f0cafab6093e19b51697892c41d4760bddba2a7faa0e3ad708c8f0abc0efb9d3b36a1cd8702e92e56e2f5ddbdf141e2388281da097e2a53cff07ff877a1df5afeb1662f0ea050ef42c6b211ea2d404962445dded692b53ecebb21174888d7872257b0a81641d7958a875249076975582c8595294e52533664280ffc75e625d31e5383ba99bcd6b1e37a683208e64008b7296c199537edf8089bc455b7b75268f517d4e4bf7017422e74ca0cffb58615f0d51ec8afa8501ec6ce66e84f1066d9001d8d145ff77aaddb146f084f90d12e898317ad943d3b5bf21b30b46759179b23cc91119c68f544d8fd6c487c9e7e291a7793d8c077d98dc25ace222b7756433919aa0fa7057edd9cb86824955ccda193bfda52d024bc6a90c2b906fe97b9e9fc37760528da46df4b44f0439537d6d2fa6937a91fc5a529a50da13daede19755cdedcd404f73c45247de1d206e1f196f598046c0216bc136d8568316a0dc59a9687f81a993a72088d18bff5c2dce8de19b4eb9056dd3420c9b12a30a44310d7dfd00adbaf3048489cff3d555095d52342ef27ae720976364c87968e1b67fb5a669cd94fef0e7f1b0eeacd5b110d60ee5b2f2431bcfafec40c4a5bdf9b96281960538969d69e87da799238004c0110365a8a46131a1e5d665cdcaa64da978c34dd636dae26768908f65f460bcaa580a8b000e2dfa5fde48ab5f965b4a665c1527b97aebe1d52e7d767bf247f74edd4bcb3e07bc0077e4f01bf59921bcb3d9030afa952089f77f8d9063a7934a082815e135b378f30db7fb91fec5140474d157b0ac178d46b54a50e33b0de8a94b17bbe19b94a3dd6602fb888ab0179978097a3d4ca883e62f98954453e6010877ef39519ec51fc94965d1d282ff66962600c03ef01d4deb6195681510d07bd88f63239f818bf13e37356ca09a7e67fd2afee38e0c05c4c6d21e9a8665d14f8f06fe33fa63e152e0a1adb4ecaa44c41f09501c97a6316ee00d87602ee07efb7a478adee08824d007358d7f86e49e53944ab4c5af17b7d96723685904bc9643489d13f8c0e0a0a586241777e11b61b9d0e2ba6167e173cc3c32ac235b94422887eee8efe3d24c4e956853d1d7f8c8bc0000cec8ac8d623608297697197f540b585b10a53e8b59bbd7e220ebb7e86d901bc8ff7d5f25adc0fec4bca22372a704719791211d2363e489eef75b5566e2cf1ed7114a0f6fcc3abb9d583e9bc5b1288f0b0e55e795683d02b43d917d3eb721ae462567a06d617f381c8a1e6c204fdc6ea107c9df1b0855d895d07f18ab3dc7dc91f54a32ff1493d1a0b382686d3fd4ae19e64759750a404d8ad1c3d8e78bfa6ae630bc1dfcb3679dbddeb7095e576be6dc83236994576948e17d4f1654b36b3e2efe07f31a1120a4e0fe3a9c47cd03381846ad8ed03df5ccc8f798accb6bd4f9d6d1febf39e3305360984d7c3229cca7e3b555ada4b06e98d5ddb1eaa42e75c463b1f39dbbe1c905ab844653c7cca335224efcdf0961df99795d50d663499222c198b1f567d6529a10b197b98319058406526064f6de7d5277d822ea2f618d01e770527232740915b24e6154c61638131429da03341a1b9ae088edea2a6ae1ea7d129edc7201d0ff968c6aabd5badfe5140b485950bc2eb7a9388deb531fc078fd8c8f7a77a588d7bf1da519d3ad2092826507fc03d3cd0e72247735af8b8f7d44f2e8018c5b443a62e586d617c79cceac7e236be6cb7b65f02a348325ae12838eb9902c4e3a0cebbf08379fdb84992b490bde654db1ea65f88c53682dbb3cd586a7a2e7aa1b10125fdf8111f971d4de7cf00706ddb5b2a1cfb08dd78477db2f2c8029242cea6b5ba4a69856d1faf3813a218ec65991fd9c125002ad9bbe462bc6b995ff3c1ed22964681d2832828dd0785528c966ccca5d13fb4c66f9b83777ab4f483a142082fad2c7ba5074b495208ee35e059469c039989fb6ddfa744e385d477fca270a7c90b345d8a5f25a65a1d2c7d7f52e5cf4fb971b22c5d56a1e5dcbc52930da25cac01542919a77d23a2df50b975b5e146d3cc69bb523d84762eab739420d55bcfcf66f67950b8a0492f63315474687b780e7c8fd930cae8d1131861f085086614d5d76f8bc288bf3498ff59f285f1502a161b5cfb3a5886365c8aaa0b183b1dca747741548124b97008dd069708bb886a6cd18f20c435e1143752b4272d4a6cf1c00879c9828e8df87b52ccff33eeaf8934e623dbb5d184c669b587f7fc458dbedc6f752d92e4ca476e3ef23a557a6da8fdfec861d617ccd80207a7e1c6b287a182ac7404b623776850b12e4b2bc814ccef6d96ad4c040a533535411e86fb8c3e282f09ed186cc40a9bc43c97a097e4dbf3c22e1a447d891d9118b234cda64afb68ca22e3a0275dec826dbb4f8456d815de41aa85a448aa4c2390e26fa2308fa25f7409e53d31cef50718df2c3ac79cd571dabfdd766be555f31ce64340702a1fbc844000100000100046a
        di "seed updated"
        local comp = "seed3"
    }

    if "$spill" == "keep" & "$stocks" == "recompute" & "$wagechange_amount" ==  "0" {
        set seed XAAc74453b136205322f0e44f31a32481711a3f4275d13555286e8d8f89925a126dacee0951859f241dac737011774e86de8598d3c105377a0aea2291d2f98756cfc4478ae02065b904948781469a4f7c25b7b768b640b689886f4e45e67482924073b187fb78b0217946de64191b9c295e1e7bb6946792cff260bf5bf722e7982259ddd3eeb23c71070e2cb60d9cc9aacf65c75c368a914e29471869a00f477ba2ffa8c973e774a2480680378dc0a63653ec4fdb9de6d01eae85b02abd4406b1c77e80fb1b65ea71f7a4e976e45f133098b12d3af3fc79f802d44c1b968ebcc7ac8f586406a1dc7f8ae0fc66ec96535a3fb8a4f8808058db6417bd5e8027e25920247f6ea195c0ac613a2521ded02cdba5d0c5f8e98cfe2358e6146ff2c36374606ef977ff60499a5d4e746c4c063124862a2a66d0a5d72554f395f4eb1dcb6cc2b2293488afa9ce47f930c3b707e799f87af27c382732c45521d31ce9027ccf0c4f32013c7db70eb7396515013953d07c3cf3774f1bbc7facef7999071ea478ff5e3d64529f1994dbec96689744f5210e7d942827d155af44b06953954e7f6681000cde4192625b0175a3b44e22ef4f1ae4b8a57c0989b6094301ae5354727881c71cf09f0396d64b5a0ef293edb9ffa972f4b11fcdc640bf1b10b836131773dd98d195146ddec5684a421f93cacf166dcf492d26a7edc2551ab24f19ea5995bd90c5f9ea46c1e4453e617217200d6da427b3c9c7b1d3e5959c510090885942a0d64a0fa8cd58c69409f8770737e5b59e88e8a4d8be9d0bd663e0347f540963314e943566dafc0bc9b12659d099ba6dbc9eb4cfbc94e5b1e5562231682d36cee63111c4482a12fc2720269402ed5ed937fd86b53a4336c354599cbb0113a4f2a3d2a7b0fcd63e2284ac7a586330f75f33519c534107c29c45d43fb9e7a3411d5a9d5878b4f44b2ebdfa9761c882ca5c391c5b63c80e0186256cd3227c0bbcc63f705f4a4f29dc3afaf0208e8811b93b9e03c3059828fb1d1df87158f0cafab6093e19b51697892c41d4760bddba2a7faa0e3ad708c8f0abc0efb9d3b36a1cd8702e92e56e2f5ddbdf141e2388281da097e2a53cff07ff877a1df5afeb1662f0ea050ef42c6b211ea2d404962445dded692b53ecebb21174888d7872257b0a81641d7958a875249076975582c8595294e52533664280ffc75e625d31e5383ba99bcd6b1e37a683208e64008b7296c199537edf8089bc455b7b75268f517d4e4bf7017422e74ca0cffb58615f0d51ec8afa8501ec6ce66e84f1066d9001d8d145ff77aaddb146f084f90d12e898317ad943d3b5bf21b30b46759179b23cc91119c68f544d8fd6c487c9e7e291a7793d8c077d98dc25ace222b7756433919aa0fa7057edd9cb86824955ccda193bfda52d024bc6a90c2b906fe97b9e9fc37760528da46df4b44f0439537d6d2fa6937a91fc5a529a50da13daede19755cdedcd404f73c45247de1d206e1f196f598046c0216bc136d8568316a0dc59a9687f81a993a72088d18bff5c2dce8de19b4eb9056dd3420c9b12a30a44310d7dfd00adbaf3048489cff3d555095d52342ef27ae720976364c87968e1b67fb5a669cd94fef0e7f1b0eeacd5b110d60ee5b2f2431bcfafec40c4a5bdf9b96281960538969d69e87da799238004c0110365a8a46131a1e5d665cdcaa64da978c34dd636dae26768908f65f460bcaa580a8b000e2dfa5fde48ab5f965b4a665c1527b97aebe1d52e7d767bf247f74edd4bcb3e07bc0077e4f01bf59921bcb3d9030afa952089f77f8d9063a7934a082815e135b378f30db7fb91fec5140474d157b0ac178d46b54a50e33b0de8a94b17bbe19b94a3dd6602fb888ab0179978097a3d4ca883e62f98954453e6010877ef39519ec51fc94965d1d282ff66962600c03ef01d4deb6195681510d07bd88f63239f818bf13e37356ca09a7e67fd2afee38e0c05c4c6d21e9a8665d14f8f06fe33fa63e152e0a1adb4ecaa44c41f09501c97a6316ee00d87602ee07efb7a478adee08824d007358d7f86e49e53944ab4c5af17b7d96723685904bc9643489d13f8c0e0a0a586241777e11b61b9d0e2ba6167e173cc3c32ac235b94422887eee8efe3d24c4e956853d1d7f8c8bc0000cec8ac8d623608297697197f540b585b10a53e8b59bbd7e220ebb7e86d901bc8ff7d5f25adc0fec4bca22372a704719791211d2363e489eef75b5566e2cf1ed7114a0f6fcc3abb9d583e9bc5b1288f0b0e55e795683d02b43d917d3eb721ae462567a06d617f381c8a1e6c204fdc6ea107c9df1b0855d895d07f18ab3dc7dc91f54a32ff1493d1a0b382686d3fd4ae19e64759750a404d8ad1c3d8e78bfa6ae630bc1dfcb3679dbddeb7095e576be6dc83236994576948e17d4f1654b36b3e2efe07f31a1120a4e0fe3a9c47cd03381846ad8ed03df5ccc8f798accb6bd4f9d6d1febf39e3305360984d7c3229cca7e3b555ada4b06e98d5ddb1eaa42e75c463b1f39dbbe1c905ab844653c7cca335224efcdf0961df99795d50d663499222c198b1f567d6529a10b197b98319058406526064f6de7d5277d822ea2f618d01e770527232740915b24e6154c61638131429da03341a1b9ae088edea2a6ae1ea7d129edc7201d0ff968c6aabd5badfe5140b485950bc2eb7a9388deb531fc078fd8c8f7a77a588d7bf1da519d3ad2092826507fc03d3cd0e72247735af8b8f7d44f2e8018c5b443a62e586d617c79cceac7e236be6cb7b65f02a348325ae12838eb9902c4e3a0cebbf08379fdb84992b490bde654db1ea65f88c53682dbb3cd586a7a2e7aa1b10125fdf8111f971d4de7cf00706ddb5b2a1cfb08dd78477db2f2c8029242cea6b5ba4a69856d1faf3813a218ec65991fd9c125002ad9bbe462bc6b995ff3c1ed22964681d2832828dd0785528c966ccca5d13fb4c66f9b83777ab4f483a142082fad2c7ba5074b495208ee35e059469c039989fb6ddfa744e385d477fca270a7c90b345d8a5f25a65a1d2c7d7f52e5cf4fb971b22c5d56a1e5dcbc52930da25cac01542919a77d23a2df50b975b5e146d3cc69bb523d84762eab739420d55bcfcf66f67950b8a0492f63315474687b780e7c8fd930cae8d1131861f085086614d5d76f8bc288bf3498ff59f285f1502a161b5cfb3a5886365c8aaa0b183b1dca747741548124b97008dd069708bb886a6cd18f20c435e1143752b4272d4a6cf1c00879c9828e8df87b52ccff33eeaf8934e623dbb5d184c669b587f7fc458dbedc6f752d92e4ca476e3ef23a557a6da8fdfec861d617ccd80207a7e1c6b287a182ac7404b623776850b12e4b2bc814ccef6d96ad4c040a533535411e86fb8c3e282f09ed186cc40a9bc43c97a097e4dbf3c22e1a447d891d9118b234cda64afb68ca22e3a0275dec826dbb4f8456d815de41aa85a448aa4c2390e26fa2308fa25f7409e53d31cef50718df2c3ac79cd571dabfdd766be555f31ce64340702a1fbc8440001000000b206b9
        di "seed updated"
        local comp = "seed4"
    }

    if "$spill" == "keep" & "$stocks" == "keep" & "$wagechange_amount" ==  "0.1" {
        set seed XAAc74453b136205322f0e44f31a32481711a3f4275d13555286e8d8f89925a126dacee0951859f241dac737011774e86de8598d3c105377a0aea2291d2f98756cfc4478ae02065b904948781469a4f7c25b7b768b640b689886f4e45e67482924073b187fb78b0217946de64191b9c295e1e7bb6946792cff260bf5bf722e7982259ddd3eeb23c71070e2cb60d9cc9aacf65c75c368a914e29471869a00f477ba2ffa8c973e774a2480680378dc0a63653ec4fdb9de6d01eae85b02abd4406b1c77e80fb1b65ea71f7a4e976e45f133098b12d3af3fc79f802d44c1b968ebcc7ac8f586406a1dc7f8ae0fc66ec96535a3fb8a4f8808058db6417bd5e8027e25920247f6ea195c0ac613a2521ded02cdba5d0c5f8e98cfe2358e6146ff2c36374606ef977ff60499a5d4e746c4c063124862a2a66d0a5d72554f395f4eb1dcb6cc2b2293488afa9ce47f930c3b707e799f87af27c382732c45521d31ce9027ccf0c4f32013c7db70eb7396515013953d07c3cf3774f1bbc7facef7999071ea478ff5e3d64529f1994dbec96689744f5210e7d942827d155af44b06953954e7f6681000cde4192625b0175a3b44e22ef4f1ae4b8a57c0989b6094301ae5354727881c71cf09f0396d64b5a0ef293edb9ffa972f4b11fcdc640bf1b10b836131773dd98d195146ddec5684a421f93cacf166dcf492d26a7edc2551ab24f19ea5995bd90c5f9ea46c1e4453e617217200d6da427b3c9c7b1d3e5959c510090885942a0d64a0fa8cd58c69409f8770737e5b59e88e8a4d8be9d0bd663e0347f540963314e943566dafc0bc9b12659d099ba6dbc9eb4cfbc94e5b1e5562231682d36cee63111c4482a12fc2720269402ed5ed937fd86b53a4336c354599cbb0113a4f2a3d2a7b0fcd63e2284ac7a586330f75f33519c534107c29c45d43fb9e7a3411d5a9d5878b4f44b2ebdfa9761c882ca5c391c5b63c80e0186256cd3227c0bbcc63f705f4a4f29dc3afaf0208e8811b93b9e03c3059828fb1d1df87158f0cafab6093e19b51697892c41d4760bddba2a7faa0e3ad708c8f0abc0efb9d3b36a1cd8702e92e56e2f5ddbdf141e2388281da097e2a53cff07ff877a1df5afeb1662f0ea050ef42c6b211ea2d404962445dded692b53ecebb21174888d7872257b0a81641d7958a875249076975582c8595294e52533664280ffc75e625d31e5383ba99bcd6b1e37a683208e64008b7296c199537edf8089bc455b7b75268f517d4e4bf7017422e74ca0cffb58615f0d51ec8afa8501ec6ce66e84f1066d9001d8d145ff77aaddb146f084f90d12e898317ad943d3b5bf21b30b46759179b23cc91119c68f544d8fd6c487c9e7e291a7793d8c077d98dc25ace222b7756433919aa0fa7057edd9cb86824955ccda193bfda52d024bc6a90c2b906fe97b9e9fc37760528da46df4b44f0439537d6d2fa6937a91fc5a529a50da13daede19755cdedcd404f73c45247de1d206e1f196f598046c0216bc136d8568316a0dc59a9687f81a993a72088d18bff5c2dce8de19b4eb9056dd3420c9b12a30a44310d7dfd00adbaf3048489cff3d555095d52342ef27ae720976364c87968e1b67fb5a669cd94fef0e7f1b0eeacd5b110d60ee5b2f2431bcfafec40c4a5bdf9b96281960538969d69e87da799238004c0110365a8a46131a1e5d665cdcaa64da978c34dd636dae26768908f65f460bcaa580a8b000e2dfa5fde48ab5f965b4a665c1527b97aebe1d52e7d767bf247f74edd4bcb3e07bc0077e4f01bf59921bcb3d9030afa952089f77f8d9063a7934a082815e135b378f30db7fb91fec5140474d157b0ac178d46b54a50e33b0de8a94b17bbe19b94a3dd6602fb888ab0179978097a3d4ca883e62f98954453e6010877ef39519ec51fc94965d1d282ff66962600c03ef01d4deb6195681510d07bd88f63239f818bf13e37356ca09a7e67fd2afee38e0c05c4c6d21e9a8665d14f8f06fe33fa63e152e0a1adb4ecaa44c41f09501c97a6316ee00d87602ee07efb7a478adee08824d007358d7f86e49e53944ab4c5af17b7d96723685904bc9643489d13f8c0e0a0a586241777e11b61b9d0e2ba6167e173cc3c32ac235b94422887eee8efe3d24c4e956853d1d7f8c8bc0000cec8ac8d623608297697197f540b585b10a53e8b59bbd7e220ebb7e86d901bc8ff7d5f25adc0fec4bca22372a704719791211d2363e489eef75b5566e2cf1ed7114a0f6fcc3abb9d583e9bc5b1288f0b0e55e795683d02b43d917d3eb721ae462567a06d617f381c8a1e6c204fdc6ea107c9df1b0855d895d07f18ab3dc7dc91f54a32ff1493d1a0b382686d3fd4ae19e64759750a404d8ad1c3d8e78bfa6ae630bc1dfcb3679dbddeb7095e576be6dc83236994576948e17d4f1654b36b3e2efe07f31a1120a4e0fe3a9c47cd03381846ad8ed03df5ccc8f798accb6bd4f9d6d1febf39e3305360984d7c3229cca7e3b555ada4b06e98d5ddb1eaa42e75c463b1f39dbbe1c905ab844653c7cca335224efcdf0961df99795d50d663499222c198b1f567d6529a10b197b98319058406526064f6de7d5277d822ea2f618d01e770527232740915b24e6154c61638131429da03341a1b9ae088edea2a6ae1ea7d129edc7201d0ff968c6aabd5badfe5140b485950bc2eb7a9388deb531fc078fd8c8f7a77a588d7bf1da519d3ad2092826507fc03d3cd0e72247735af8b8f7d44f2e8018c5b443a62e586d617c79cceac7e236be6cb7b65f02a348325ae12838eb9902c4e3a0cebbf08379fdb84992b490bde654db1ea65f88c53682dbb3cd586a7a2e7aa1b10125fdf8111f971d4de7cf00706ddb5b2a1cfb08dd78477db2f2c8029242cea6b5ba4a69856d1faf3813a218ec65991fd9c125002ad9bbe462bc6b995ff3c1ed22964681d2832828dd0785528c966ccca5d13fb4c66f9b83777ab4f483a142082fad2c7ba5074b495208ee35e059469c039989fb6ddfa744e385d477fca270a7c90b345d8a5f25a65a1d2c7d7f52e5cf4fb971b22c5d56a1e5dcbc52930da25cac01542919a77d23a2df50b975b5e146d3cc69bb523d84762eab739420d55bcfcf66f67950b8a0492f63315474687b780e7c8fd930cae8d1131861f085086614d5d76f8bc288bf3498ff59f285f1502a161b5cfb3a5886365c8aaa0b183b1dca747741548124b97008dd069708bb886a6cd18f20c435e1143752b4272d4a6cf1c00879c9828e8df87b52ccff33eeaf8934e623dbb5d184c669b587f7fc458dbedc6f752d92e4ca476e3ef23a557a6da8fdfec861d617ccd80207a7e1c6b287a182ac7404b623776850b12e4b2bc814ccef6d96ad4c040a533535411e86fb8c3e282f09ed186cc40a9bc43c97a097e4dbf3c22e1a447d891d9118b234cda64afb68ca22e3a0275dec826dbb4f8456d815de41aa85a448aa4c2390e26fa2308fa25f7409e53d31cef50718df2c3ac79cd571dabfdd766be555f31ce64340702a1fbc844000100000100046a
        di "seed updated"
        local comp = "seed5"
    }

    if "$spill" == "keep" & "$stocks" == "keep" & "$wagechange_amount" ==  "0" {
        set seed XAAc74453b136205322f0e44f31a32481711a3f4275d13555286e8d8f89925a126dacee0951859f241dac737011774e86de8598d3c105377a0aea2291d2f98756cfc4478ae02065b904948781469a4f7c25b7b768b640b689886f4e45e67482924073b187fb78b0217946de64191b9c295e1e7bb6946792cff260bf5bf722e7982259ddd3eeb23c71070e2cb60d9cc9aacf65c75c368a914e29471869a00f477ba2ffa8c973e774a2480680378dc0a63653ec4fdb9de6d01eae85b02abd4406b1c77e80fb1b65ea71f7a4e976e45f133098b12d3af3fc79f802d44c1b968ebcc7ac8f586406a1dc7f8ae0fc66ec96535a3fb8a4f8808058db6417bd5e8027e25920247f6ea195c0ac613a2521ded02cdba5d0c5f8e98cfe2358e6146ff2c36374606ef977ff60499a5d4e746c4c063124862a2a66d0a5d72554f395f4eb1dcb6cc2b2293488afa9ce47f930c3b707e799f87af27c382732c45521d31ce9027ccf0c4f32013c7db70eb7396515013953d07c3cf3774f1bbc7facef7999071ea478ff5e3d64529f1994dbec96689744f5210e7d942827d155af44b06953954e7f6681000cde4192625b0175a3b44e22ef4f1ae4b8a57c0989b6094301ae5354727881c71cf09f0396d64b5a0ef293edb9ffa972f4b11fcdc640bf1b10b836131773dd98d195146ddec5684a421f93cacf166dcf492d26a7edc2551ab24f19ea5995bd90c5f9ea46c1e4453e617217200d6da427b3c9c7b1d3e5959c510090885942a0d64a0fa8cd58c69409f8770737e5b59e88e8a4d8be9d0bd663e0347f540963314e943566dafc0bc9b12659d099ba6dbc9eb4cfbc94e5b1e5562231682d36cee63111c4482a12fc2720269402ed5ed937fd86b53a4336c354599cbb0113a4f2a3d2a7b0fcd63e2284ac7a586330f75f33519c534107c29c45d43fb9e7a3411d5a9d5878b4f44b2ebdfa9761c882ca5c391c5b63c80e0186256cd3227c0bbcc63f705f4a4f29dc3afaf0208e8811b93b9e03c3059828fb1d1df87158f0cafab6093e19b51697892c41d4760bddba2a7faa0e3ad708c8f0abc0efb9d3b36a1cd8702e92e56e2f5ddbdf141e2388281da097e2a53cff07ff877a1df5afeb1662f0ea050ef42c6b211ea2d404962445dded692b53ecebb21174888d7872257b0a81641d7958a875249076975582c8595294e52533664280ffc75e625d31e5383ba99bcd6b1e37a683208e64008b7296c199537edf8089bc455b7b75268f517d4e4bf7017422e74ca0cffb58615f0d51ec8afa8501ec6ce66e84f1066d9001d8d145ff77aaddb146f084f90d12e898317ad943d3b5bf21b30b46759179b23cc91119c68f544d8fd6c487c9e7e291a7793d8c077d98dc25ace222b7756433919aa0fa7057edd9cb86824955ccda193bfda52d024bc6a90c2b906fe97b9e9fc37760528da46df4b44f0439537d6d2fa6937a91fc5a529a50da13daede19755cdedcd404f73c45247de1d206e1f196f598046c0216bc136d8568316a0dc59a9687f81a993a72088d18bff5c2dce8de19b4eb9056dd3420c9b12a30a44310d7dfd00adbaf3048489cff3d555095d52342ef27ae720976364c87968e1b67fb5a669cd94fef0e7f1b0eeacd5b110d60ee5b2f2431bcfafec40c4a5bdf9b96281960538969d69e87da799238004c0110365a8a46131a1e5d665cdcaa64da978c34dd636dae26768908f65f460bcaa580a8b000e2dfa5fde48ab5f965b4a665c1527b97aebe1d52e7d767bf247f74edd4bcb3e07bc0077e4f01bf59921bcb3d9030afa952089f77f8d9063a7934a082815e135b378f30db7fb91fec5140474d157b0ac178d46b54a50e33b0de8a94b17bbe19b94a3dd6602fb888ab0179978097a3d4ca883e62f98954453e6010877ef39519ec51fc94965d1d282ff66962600c03ef01d4deb6195681510d07bd88f63239f818bf13e37356ca09a7e67fd2afee38e0c05c4c6d21e9a8665d14f8f06fe33fa63e152e0a1adb4ecaa44c41f09501c97a6316ee00d87602ee07efb7a478adee08824d007358d7f86e49e53944ab4c5af17b7d96723685904bc9643489d13f8c0e0a0a586241777e11b61b9d0e2ba6167e173cc3c32ac235b94422887eee8efe3d24c4e956853d1d7f8c8bc0000cec8ac8d623608297697197f540b585b10a53e8b59bbd7e220ebb7e86d901bc8ff7d5f25adc0fec4bca22372a704719791211d2363e489eef75b5566e2cf1ed7114a0f6fcc3abb9d583e9bc5b1288f0b0e55e795683d02b43d917d3eb721ae462567a06d617f381c8a1e6c204fdc6ea107c9df1b0855d895d07f18ab3dc7dc91f54a32ff1493d1a0b382686d3fd4ae19e64759750a404d8ad1c3d8e78bfa6ae630bc1dfcb3679dbddeb7095e576be6dc83236994576948e17d4f1654b36b3e2efe07f31a1120a4e0fe3a9c47cd03381846ad8ed03df5ccc8f798accb6bd4f9d6d1febf39e3305360984d7c3229cca7e3b555ada4b06e98d5ddb1eaa42e75c463b1f39dbbe1c905ab844653c7cca335224efcdf0961df99795d50d663499222c198b1f567d6529a10b197b98319058406526064f6de7d5277d822ea2f618d01e770527232740915b24e6154c61638131429da03341a1b9ae088edea2a6ae1ea7d129edc7201d0ff968c6aabd5badfe5140b485950bc2eb7a9388deb531fc078fd8c8f7a77a588d7bf1da519d3ad2092826507fc03d3cd0e72247735af8b8f7d44f2e8018c5b443a62e586d617c79cceac7e236be6cb7b65f02a348325ae12838eb9902c4e3a0cebbf08379fdb84992b490bde654db1ea65f88c53682dbb3cd586a7a2e7aa1b10125fdf8111f971d4de7cf00706ddb5b2a1cfb08dd78477db2f2c8029242cea6b5ba4a69856d1faf3813a218ec65991fd9c125002ad9bbe462bc6b995ff3c1ed22964681d2832828dd0785528c966ccca5d13fb4c66f9b83777ab4f483a142082fad2c7ba5074b495208ee35e059469c039989fb6ddfa744e385d477fca270a7c90b345d8a5f25a65a1d2c7d7f52e5cf4fb971b22c5d56a1e5dcbc52930da25cac01542919a77d23a2df50b975b5e146d3cc69bb523d84762eab739420d55bcfcf66f67950b8a0492f63315474687b780e7c8fd930cae8d1131861f085086614d5d76f8bc288bf3498ff59f285f1502a161b5cfb3a5886365c8aaa0b183b1dca747741548124b97008dd069708bb886a6cd18f20c435e1143752b4272d4a6cf1c00879c9828e8df87b52ccff33eeaf8934e623dbb5d184c669b587f7fc458dbedc6f752d92e4ca476e3ef23a557a6da8fdfec861d617ccd80207a7e1c6b287a182ac7404b623776850b12e4b2bc814ccef6d96ad4c040a533535411e86fb8c3e282f09ed186cc40a9bc43c97a097e4dbf3c22e1a447d891d9118b234cda64afb68ca22e3a0275dec826dbb4f8456d815de41aa85a448aa4c2390e26fa2308fa25f7409e53d31cef50718df2c3ac79cd571dabfdd766be555f31ce64340702a1fbc8440001000000b206b9
        di "seed updated"
        local comp = "seed6"
    }
    global seed_state = c(seed)
    di "Seed state: ${seed_state}"
    di "comp is `comp'"
  

    save ${outd}/data/simdata_${sname}.dta, replace





* ---------------------------- *
* ---------------------------- *
* 2. SIMULATION
* ---------------------------- *
* ---------------------------- *

di "=== Starting Simulation ==="


forvalues i = 1/$N {
    global j = `i'
    use ${outd}/data/simdata_${sname}.dta, clear

    di "=========== Batch ${batchrun}, Run #`i' / $N, Diagnostics ${diagnn} =============="
    di "Config: $chosen_spec$FE, $stocks$spill, $spillrcvers$iwvers$osmtrim, $wagechange $ssel"

    qui gen pdepvar = depvar 

    if "$stocks" == "recompute" {

        forvalues yy = 1995/2009 { 

            di "=== Recomputing stock/spills. Run `i' / $N. Year `yy' / 2009 ==="

           qui { 
            *the files below do exactly what it says on the tin.
            *patenting is done per year implemented and then we predict the next year
                do "${d}code/macrosim/predict_patenting.do" `yy'

                if "$spill" == "recompute" { 
                    * recompute spillovers 
                    do "${d}code/macrosim/recompute_spillovers.do" pdepvar 
                    do "${d}code/macrosim/implement_spillovers.do"
                }

                * re-define stocks
                do "${d}code/macrosim/implement_stocks.do"
           }        
        } 
    }
    else {  
        qui { 
            * direct effect, no recomputation of stocks or spillovers
            predict pred
            replace pred = 0 if pred == .
            replace pdepvar = rpoisson(L2.pred)
            replace pdepvar = 0 if pdepvar == .
        }
    }

    * save run with an indicator on which run we are saving
    qui do "${d}code/macrosim/save_run.do" `i'
}


* -----------------------------
* 3. Combine runs, save results
* -----------------------------


di "=== Finished. Saving batch runs ==="

qui { 
    * merge the actual depvar back into the predicted data
    * collapse for graphing, save batch runs
    sleep 4000
    use ${outd}/data/simdata_${sname}.dta, clear
    keep if ctryid == 1
    keep if year >= 1997 & year <= 2011
    collapse (sum) depvar, by(year division) 
    tempfile depvar
    save `depvar', replace

    use ${outd}/data/batchruns_${bname}.dta, clear
    mmerge year division using `depvar', unmatched(none)
    drop _m

    compress
    sleep 4000
    save ${outd}/data/batchruns_${bname}.dta, replace
}